﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Login : MonoBehaviour
{
    public TweenScale m_signInWidget;
    public TweenScale m_signUpWidget;
    public TweenScale m_ChooseServer;

    //public delegate void void_gobj_func(GameObject obj);

    public UIInput m_signUpAccountStr;
    public UIInput m_signUpPasswordStr;
    public UIInput m_signUpConfirmStr;
    public UIInput m_signUpPhoneStr;

    public UIInput m_loginAccountStr;
    public UIInput m_loginPasswordStr;

    void Awake()
    {
        Invoke("register_KB_Events", 0.5f);
    }
    // Start is called before the first frame update
    void Start()
    {
        GameObject tempObj = UnityEngine.Resources.Load<GameObject>("MessageHint");
        if (tempObj)
        {
            print("1111111111111111111111111\n");
            GameObject obj = Instantiate(tempObj) as GameObject;
            //obj.transform.localScale = tempObj.transform.localScale;
            obj.transform.SetParent(GameObject.Find("UI Root").gameObject.transform,false);
            obj.transform.position.Normalize();
            obj.SetActive(true);
        }
        else
        {
            print("222222222222222222222222\n");
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void register_KB_Events()
    {
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onCreateAccountResult,this, "onCreateAccountResult");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onBindAccountEmail, this, "onBindAccountEmail");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onNewPassword, this, "onNewPassword");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onResetPassword, this, "onResetPassword");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onKicked, this, "onKicked");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onDisconnected, this, "onDisconnected");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onConnectionState, this, "onConnectionState");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onVersionNotMatch, this, "onVersionNotMatch");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onScriptVersionNotMatch, this, "onScriptVersionNotMatch");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onLoginFailed, this, "onLoginFailed");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onLoginBaseapp, this, "onLoginBaseapp");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onLoginBaseappFailed, this, "onLoginBaseappFailed");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onReloginBaseapp, this, "onReloginBaseapp");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onReloginBaseappSuccessfully, this, "onReloginBaseappSuccessfully");
        KBEngine.Event.registerOut(KBEngine.EventOutTypes.onReloginBaseappFailed, this, "onReloginBaseappFailed");
    }

    public void onBindAccountEmail(UInt16 param)
    {
        Debug.Log("onBindAccountEmail param = " + param);
    }
    public void onNewPassword(UInt16 param)
    {
        Debug.Log("onNewPassword param = " + param);
    }
    public void onResetPassword(UInt16 param)
    {
        Debug.Log("onResetPassword param = " + param);
    }
    public void onKicked(UInt16 param)
    {
        Debug.Log("onKicked param = " + param);
    }
    public void onDisconnected()
    {
        Debug.Log("onDisconnected ");
    }
    public void onConnectionState(bool param)
    {
        Debug.Log("onConnectionState param = " + param);
    }
    public void onVersionNotMatch(string param1,string param2)
    {
        Debug.Log("onVersionNotMatch clientVersion = " + param1 + "serverVersion = " + param2);
    }
    public void onScriptVersionNotMatch(string param1, string param2)
    {
        Debug.Log("onVersionNotMatch clientScriptVersion = " + param1 + "serverScriptVersion = " + param2);
    }
    public void onLoginFailed(UInt16 param)
    {
        Debug.Log("onLoginFailed param = " + param);
    }
    public void onLoginBaseapp()
    {
        Debug.Log("onLoginBaseapp ");
        if (m_signUpWidget.gameObject.activeInHierarchy)
        {
            m_signInWidget.gameObject.SetActive(false);
        }
        if (m_signInWidget.gameObject.activeInHierarchy)
        {
            m_signInWidget.PlayReverse();
            StartCoroutine(delayDoing(m_signInWidget.gameObject, 0.3f, (obj) =>
             {
                 m_signInWidget.gameObject.SetActive(false);
                 m_ChooseServer.gameObject.SetActive(true);
                 m_ChooseServer.PlayForward();
             }));
        }

    }
    public void onLoginBaseappFailed(UInt16 param)
    {
        Debug.Log("onLoginBaseappFailed param = " + param);
    }
    public void onReloginBaseapp()
    {
        Debug.Log("onReloginBaseapp ");
    }
    public void onReloginBaseappSuccessfully()
    {
        Debug.Log("onReloginBaseappSuccessfully ");
    }
    public void onReloginBaseappFailed(UInt16 param)
    {
        Debug.Log("onReloginBaseappFailed param = " + param);
    }

    public void onSignUpBtClicked()
    {
        m_signInWidget.PlayForward();
        StartCoroutine(delayDoing(m_signInWidget.gameObject, 0.3f, (obj) => 
        {
            m_signInWidget.gameObject.SetActive(false);
            m_signUpWidget.gameObject.SetActive(true);
            m_signUpWidget.PlayForward();
        }));

    }

    public void onSignUpCloseCliked()
    {
        m_signUpWidget.PlayReverse();
        StartCoroutine(delayDoing(m_signUpWidget.gameObject, 0.3f, (obj) =>
        {
            m_signUpWidget.gameObject.SetActive(false);
            m_signInWidget.gameObject.SetActive(true);
            m_signInWidget.PlayReverse();
        }));
    }

    private bool checkSignUp()
    {
        if (m_signUpAccountStr.value.Length < 3)
            return false;
        if (!m_signUpPasswordStr.value.Equals(m_signUpConfirmStr.value))
            return false;
        if (m_signUpPhoneStr.value.Length != 11)
            return false;
        return true;
    }
    public void onSignUpClicked()
    {
        if (checkSignUp())
            KBEngine.Event.fireIn(KBEngine.EventInTypes.createAccount, m_signUpAccountStr.value, m_signUpPasswordStr.value, System.Text.Encoding.UTF8.GetBytes(m_signUpPhoneStr.value));
    }

    public void onLoginBtClicked()
    {
        Debug.Log("onLoginBtClicked ");
        KBEngine.Event.fireIn(KBEngine.EventInTypes.login, m_loginAccountStr.value, m_loginPasswordStr.value, new byte[] { });
    }

    //public static void StartYourShow(System.Func<bool> CanEnd, System.Action Method, System.Action EndMethod)
    //{
    //    while (!CanEnd()) Method();
    //    EndMethod();
    //}
    //public static void StartYourShow(System.Func<int, bool> CanEnd, System.Action<string> Method, System.Action<string, string> EndMethod)
    //{
    //    while (!CanEnd(10))
    //        Method(DateTime.Now.TimeOfDay.ToString());
    //    EndMethod("Show", "End");
    //}



    public void onCreateAccountResult(UInt16 server_error,byte [] datas)
    {
        print("---------------------------------------\n");
        Debug.Log("onCreateAccountResult -- error datas" + server_error + datas);
    }


    IEnumerator delayDoing(GameObject obj,float delay, System.Action<GameObject> func)
    {
        yield return new WaitForSeconds(delay);
        func.Invoke(obj);
    }
}
